package org.lanqiao.tjut.Controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.tjut.Bean.StudentBean;
import org.lanqiao.tjut.MyUtils.MyUtils;
import org.lanqiao.tjut.Service.ServiceFactory;

public class StuInfoModifyServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 准备用户进行修改的数据
		String strOperationType = request.getParameter("opt");
		int iOpt = MyUtils.obj2Int(strOperationType);
		// 结果跳转页面
		String strJumpPage = "";

		if (iOpt == 0) {
			// 修改跳转操作
			// 获取主键ID
			String strUid = request.getParameter("uid");
			StudentBean stuB = ServiceFactory.getStuInfoManagerServiceInstance().getStuInfoById(strUid);
			// 将数据缓存在session对象中
			request.setAttribute("stuB", stuB);

			// 修改性别输入框为选择框

			// 跳转到修改结果页
			strJumpPage = "logined/UserInfoManager/stuInfoAdd.jsp";
		} else if (iOpt == 1) {
			// 删除

			int stuId = MyUtils.obj2Int(request.getParameter("uid"));
			// 数据库删除
			int i = ServiceFactory.getStuInfoManagerServiceInstance().deleteStuById(stuId);
			// 跳转页面
			strJumpPage = "StuInfoModifyServlet?opt=2";
		} else if (iOpt == 2) {
			// 判定当前的操作是查询还是修改跳转
			// 查询操作
			// 2、将保单数据填充到实体类
			StudentBean stuB = (StudentBean) MyUtils.getNewInstance(StudentBean.class);
			stuB.setStuname(request.getParameter("txtStuName"));
			stuB.setStuage(MyUtils.obj2Int(request.getParameter("txtStuAge")));
			stuB.setStuage2(MyUtils.obj2Int(request.getParameter("txtStuAge2")));
			stuB.setStuaddress(request.getParameter("txtStuAddress"));
			stuB.setStusex(request.getParameter("selectStuSex"));
			stuB.setStutel(request.getParameter("txtStuTel"));
			// 查询操作
			List<StudentBean> lstStu = ServiceFactory.getStuInfoManagerServiceInstance().doQueryByParas(stuB);
			// 将需要显示的结果集放入session缓存中
			request.getSession().setAttribute("lstStu", lstStu);
			// 查询结果页
			strJumpPage = "logined/UserInfoManager/stuInfoDetail.jsp";
		}
		// 跳转到修改页面
		request.getRequestDispatcher(strJumpPage).forward(request, response);
	}
}
